package fr.resasante.utils;

import javax.naming.NamingException;
import javax.servlet.http.HttpServletRequest;

public final class SessionUtils {
	/**
	 * Class instance
	 */
	private static volatile SessionUtils instance = null;

	/**
	 * Constructor
	 * 
	 * @throws NamingException
	 */
	private SessionUtils() {
		super();
	}

	/**
	 * Method returning a Singleton class instance
	 * 
	 * @return Singleton instance
	 * @throws NamingException
	 */
	public final static SessionUtils getInstance() {
		if (SessionUtils.instance == null) {
			synchronized (SessionUtils.class) {
				if (SessionUtils.instance == null) {
					SessionUtils.instance = new SessionUtils();
				}
			}
		}
		return SessionUtils.instance;
	}

	/**
	 * Get a attribute in session
	 * 
	 * @param request
	 *            The request
	 * @param key
	 *            The requested key
	 * @return The value
	 */
	public Object getSessionAttribute(HttpServletRequest request, String key) {
		return request.getSession().getAttribute(key);
	}

	/**
	 * Set a value with a key in session
	 * 
	 * @param request
	 *            The request
	 * @param key
	 *            The key
	 * @param value
	 *            The value
	 */
	public void setSessionAttribute(HttpServletRequest request, String key, Object value) {
		request.getSession().setAttribute(key, value);
	}

	/**
	 * Get a attribute in request
	 * 
	 * @param request
	 *            The request
	 * @param key
	 *            The requested key
	 * @return The value
	 */
	public Object getRequestAttribute(HttpServletRequest request, String key) {
		return request.getAttribute(key);
	}

	/**
	 * Set a value with a key in request
	 * 
	 * @param request
	 *            The request
	 * @param key
	 *            The key
	 * @param value
	 *            The value
	 */
	public void setRequestAttribute(HttpServletRequest request, String key, Object value) {
		request.setAttribute(key, value);
	}
}